package room

import (
	"errors"
	"server/common"

	"github.com/mitchellh/mapstructure"
	"github.com/sirupsen/logrus"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) handleUserOfflineNotify(msg common.Message, from common.ServiceInfo,
	connId string) error {
	LOG().WithFields(logrus.Fields{
		"msg":    common.ToJson(msg),
		"from":   from,
		"connId": connId,
	}).Info("Handling user offline notify")

	var data common.UserOfflineNotifyData
	if err := mapstructure.Decode(msg.Data, &data); err != nil {
		return errors.New("failed to decode user offline notify data: " + err.Error())
	}

	if data.UserId == "" {
		return errors.New("user_id is required")
	}

	s.roomMgr.OnUserOffline(data.UserId)

	return nil
}
